package com.scud.system.controller;


import com.scud.common.result.Result;
import com.scud.model.PageResult;
import com.scud.model.system.dto.AssignPermissionDto;
import com.scud.model.system.dto.RolePageDto;
import com.scud.model.system.pojo.Role;
import com.scud.system.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/system/role")
@Slf4j
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 条件查询角色
     *
     * @param page
     * @param limit
     * @param rolePageDto
     * @return
     */
    @PostMapping("/page/{page}/{limit}")
    @PreAuthorize("hasAuthority('ROLE_LIST')")
    public Result<PageResult<Role>> page(@PathVariable Integer page,
                                         @PathVariable Integer limit,
                                         @RequestBody RolePageDto rolePageDto) {
        log.info("条件查询角色:{},{},{}", page, limit, rolePageDto);
        PageResult<Role> res = roleService.page(page, limit, rolePageDto);
        return Result.success(res);
    }

    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    public Result save(@RequestBody Role role) {
        log.info("新增角色:{}", role);
        roleService.save(role);
        return Result.success();
    }

    /**
     * 修改角色
     *
     * @param role
     * @return
     */
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('ROLE_UPDATE')")
    public Result update(@RequestBody Role role) {
        log.info("修改角色:{}", role);
        roleService.update(role);
        return Result.success();
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result delete(@PathVariable Long id) {
        log.info("删除角色:{}", id);
        roleService.deleteById(id);
        return Result.success();
    }

    /**
     * 获取 全部角色 和 userId对应角色id
     * allRoles   userRoleIds
     *
     * @return
     */
    @GetMapping("/getAllRoles/{userId}")
    @PreAuthorize("hasAuthority('ROLE_LIST')")
    public Result<Map<String, Object>> getAllRoles(@PathVariable Long userId) {
        log.info("获取全部角色及userId:{}对应角色id", userId);
        Map<String, Object> map = roleService.getAllRoles(userId);
        return Result.success(map);
    }

    /**
     * 为角色分配权限
     *
     * @param assignPermissionDto
     * @return
     */
    @PostMapping("/assign")
    @PreAuthorize("hasAuthority('ROLE_ASSIGN_PER')")
    public Result assign(@RequestBody AssignPermissionDto assignPermissionDto) {
        log.info("为角色分配权限:{}", assignPermissionDto);
        roleService.assign(assignPermissionDto);
        return Result.success();
    }
}
